
    class NodeList {
        public static ListNode reverseKGroup(ListNode head, int k)
        {
            ListNode start = head;
            ListNode end=find_group_end(head,k);
            head = end;
            ListNode pre_end = null;

            while(end!=null)
            {
                ListNode nextStart = end.next;
                ListNode p = reverseSubList(start,end);
                if (pre_end!=null) pre_end.next=p;
                pre_end = start;
                System.out.println(pre_end.val);
                start = nextStart;
                end = find_group_end(start,k);
            }

            return head;

        }

        public static ListNode reverseSubList(ListNode start, ListNode end)
        {
            ListNode nextStart = end.next;
            ListNode pre = start;
            ListNode now = null;
            ListNode next = null;

            now = pre.next;
            //next = now.next;

            while(pre!=end)
            {
                next=now.next;
                now.next=pre;
                pre=now;
                now=next;
            }

            start.next=nextStart;
            start = end;
            return start;
        }

        public static ListNode find_group_end(ListNode start, int k)
        {
            ListNode p = start;
            for (int i = 0; i < k-1;i++)
            {
                if(p==null) return null;
                p = p.next;
            }
            return p;
        }

        public static void printList(ListNode head)
        {
            ListNode p = head;
            while(p!=null)
            {
                System.out.print(p.val+" ");
                p = p.next;
            }
        }

        public static ListNode buildList(int[] arr)
        {
            int len = arr.length;
            ListNode next = null;
            ListNode now = null;
            for(int i=len-1; i>=0; i--)
            {
                now = new ListNode(arr[i],next);
                next = now;
            }
            return now;
        }

        public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {

            if(l1==null) return l2;
            if(l2==null) return l1;

            int len1 = nodeListLen(l1);
            int len2 = nodeListLen(l2);
            ListNode L;
            ListNode S;
            if(len1>=len2){L=l1;S=l2;}
            else{L=l2;S=l1;}

            int carry=0;

            ListNode p = L;
            ListNode q = S;

            while(p!=null || carry!=0)
            {
                int val1=0;
                int val2=0;
                val1 = p.val;
                if(q!=null){val2=q.val;q=q.next;}else{val2=0;}
                p.val = (val1+val2+carry)%10;
                carry = (val1+val2+carry)/10; System.out.println(carry+" "+val1+" "+val2);
                if(p.next==null && carry!=0){p.next=new ListNode(0,null);}
                p = p.next;
            }
            return L;
        }

        public static int nodeListLen(ListNode head)
        {
            ListNode p = head;
            int count = 0;
            while(p!=null){count++;p=p.next;}
            return count;
        }

        public static void main(String[] args)
        {
            int[] arr1 = {3,6,3};
            int[] arr2 = {3,6,7};
            ListNode l1 = buildList(arr1);
            ListNode l2 = buildList(arr2);

            ListNode ans = addTwoNumbers(l1,l2);
            printList(ans);

        }
    }
